package cc.zoyn.momentimageclubclient.builder;


import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.shapes.Shape;
import android.support.annotation.DrawableRes;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import cc.zoyn.momentimageclubclient.R;

/**
 * SnackbarBuilder - Builder
 *
 * @author Zoyn
 * @since 2017-10-21
 */
public class SnackbarBuilder {

    /**
     * 默认颜色
     */
    private static final int BLUE = 0xff2195f3;
    private static final int WHITE = 0xffffffff;
    private static final int ORANGE = 0xffffc107;

    /**
     * 相关变量
     */
    private Snackbar currentSnackbar;
    private String message;
    private int duration = Snackbar.LENGTH_SHORT;

    private int messageColor = WHITE;
    private int backgroundColor = BLUE;

    private SnackbarAction snackbarAction;

    private SnackbarBuilder(View view) {
        currentSnackbar = Snackbar.make(view, this.message, duration);
    }

    private SnackbarBuilder(View view, String message) {
        currentSnackbar = Snackbar.make(view, this.message, duration);
    }

    private SnackbarBuilder(View view, String message, int duration) {
        currentSnackbar = Snackbar.make(view, this.message, duration);
    }

    public static SnackbarBuilder createSnackBar(View view) {
        return new SnackbarBuilder(view);
    }

    public static SnackbarBuilder createSnackBar(View view, String message) {
        return new SnackbarBuilder(view, message);
    }

    public static SnackbarBuilder createSnackBar(View view, String message, int duration) {
        return new SnackbarBuilder(view, message, duration);
    }

    public SnackbarBuilder setAction(String displayText, View.OnClickListener onClickListener) {
        this.snackbarAction = new SnackbarAction(displayText, onClickListener);
        return this;
    }

    public SnackbarBuilder setAction(SnackbarAction snackbarAction) {
        this.snackbarAction = snackbarAction;
        return this;
    }

    public SnackbarBuilder setMessage(String message) {
        this.message = message;
        return this;
    }

    public SnackbarBuilder setDuration(int duration) {
        this.duration = duration;
        return this;
    }

    public SnackbarBuilder setMessageColor(int messageColor) {
        this.messageColor = messageColor;
        return this;
    }

    public SnackbarBuilder setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        return this;
    }

    private SnackbarBuilder setLeftIcon(@DrawableRes int icon) {
        View view = currentSnackbar.getView();
        Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) view;

        View addView = LayoutInflater.from(view.getContext()).inflate(icon, null);
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        p.gravity = Gravity.CENTER_VERTICAL;
        snackbarLayout.addView(addView, 0, p);
        return this;
    }

    public void show() {
        // set colors
        View view = currentSnackbar.getView();
        view.setBackgroundColor(backgroundColor);
        TextView textView = (TextView) view.findViewById(R.id.snackbar_text);
        textView.setTextColor(messageColor);

        // set text
        currentSnackbar.setText(message);

        // action
        if (snackbarAction != null) {
            currentSnackbar.setAction(snackbarAction.getDisplayText(), snackbarAction.getOnClickListener());
            currentSnackbar.setActionTextColor(snackbarAction.getActionColor());
        }

        // duration
        currentSnackbar.setDuration(duration);

        currentSnackbar.show();
    }

    public static class SnackbarAction {
        private int actionColor = ORANGE;
        private String displayText;
        private View.OnClickListener onClickListener;

        private SnackbarAction(String displayText, View.OnClickListener onClickListener) {
            this.displayText = displayText;
            this.onClickListener = onClickListener;
        }

        public static SnackbarAction createAction(String displayText, View.OnClickListener onClickListener) {
            return new SnackbarAction(displayText, onClickListener);
        }

        public int getActionColor() {
            return actionColor;
        }

        public String getDisplayText() {
            return displayText;
        }

        public View.OnClickListener getOnClickListener() {
            return onClickListener;
        }

        public SnackbarAction setActionColor(int actionColor) {
            this.actionColor = actionColor;
            return this;
        }

        public SnackbarAction setDisplayText(String displayText) {
            this.displayText = displayText;
            return this;
        }

        public SnackbarAction setOnClickListener(View.OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
            return this;
        }
    }
}
